﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace kompot_wtf
{
    public class MobAnimation
    {
        private Texture2D texture;
        private Vector2 frameSize;
        private int frameLine;
        private int currentFrame;
        private int frameChange = 1;
        private bool flip;
        private bool reversed;

        public MobAnimation(Texture2D texture, Vector2 frameSize)
        {
            this.texture = texture;
            this.frameSize = frameSize;
            this.currentFrame = 2;
            this.frameLine = 0;
            this.flip = false;
            this.reversed = false;
            this.direction = MobAnimationDirection.Bottom;
        }

        public void ChangeDirection(MobAnimationDirection newDirection)
        {
            direction = newDirection;

            switch (direction)
            {
                case MobAnimationDirection.Bottom:
                    flip = false;
                    frameLine = 0;
                    break;

                case MobAnimationDirection.Top:
                    flip = false;
                    frameLine = 1;
                    break;

                case MobAnimationDirection.Left:
                    flip = false;
                    frameLine = 2;
                    break;

                case MobAnimationDirection.BottomLeft:
                    flip = false;
                    frameLine = 3;
                    break;

                case MobAnimationDirection.TopLeft:
                    flip = true;
                    frameLine = 4;
                    break;

                case MobAnimationDirection.BottomRight:
                    flip = true;
                    frameLine = 3;
                    break;

                case MobAnimationDirection.Right:
                    flip = true;
                    frameLine = 2;
                    break;

                case MobAnimationDirection.TopRight:
                    flip = false;
                    frameLine = 4;
                    break;
            }
        }

        public void NextFrame()
        {
            if (currentFrame == 4)
            {
                currentFrame = 3;
                frameChange = -1;
            }
            else if (currentFrame == 0)
            {
                currentFrame = 1;
                frameChange = 1;
            }
            else
            {
                currentFrame += frameChange;
            }
        }

        public void Reversed()
        {
            if (!reversed)
            {
                frameChange = -frameChange;
                reversed = true;
            }
        }

        public void Normal()
        {
            if (reversed)
            {
                frameChange = -frameChange;
                reversed = false;
            }
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position)
        {
            spriteBatch.Begin();

            var destRect = GetDestRect(position);
            var srcRect = GetSrcRect();
            var origin = new Vector2(0, 0);
            var effects = flip ? SpriteEffects.FlipHorizontally : SpriteEffects.None;

            spriteBatch.Draw(texture, destRect, srcRect, Color.White, 0, origin, effects, 0);

            spriteBatch.End();
        }

        private Rectangle GetDestRect(Vector2 pos)
        {
            int x = (int)(pos.X - frameSize.X / 2);
            int y = (int)(pos.Y - frameSize.Y / 2);
            int sizeX = (int)frameSize.X;
            int sizeY = (int)frameSize.Y;

            return new Rectangle(x, y, sizeX, sizeY);
        }

        private Rectangle GetSrcRect()
        {
            int x = (int)(currentFrame * frameSize.X);
            int y = (int)(frameLine * frameSize.Y);
            int sizeX = (int)frameSize.X;
            int sizeY = (int)frameSize.Y;

            return new Rectangle(x, y, sizeX, sizeY);
        }

        private MobAnimationDirection direction;
        public MobAnimationDirection Direction
        {
            get { return direction; }
        }
    }
}
